import java.awt.Color;
enum Direction{//表示方向
    RIGHTUP,
    RIGHTDOWN,
    LEFTDOWN,
    LEFTUP
};//小球的运动方向

public class Ball {//小球
    private int x,y,r;//X坐标，Y坐标，半径
    private int fieldX,fieldY;
    private Direction direction;
    private int speed=1;

    public Ball(int x, int y, int r,int fieldX,int fieldY,Direction direction,int speed ) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.fieldX = fieldX;
        this.fieldY = fieldY;
        this.direction = direction;
        this.speed = speed;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void move(){
        //如果没有碰到墙壁
        if((x-r>0)&&(y-r>0)&&(x+r<fieldX)&&(y+r<fieldY)){
            //根据方向计算新坐标
            switch(direction){
                case RIGHTUP:
                    x+= speed;
                    y-= speed;
                    break;
                case RIGHTDOWN:
                    x+=speed;
                    y+=speed;
                    break;
                case LEFTDOWN:
                    x-=speed;
                    y+=speed;
                    break;
                case LEFTUP:
                    x-=speed;
                    y-=speed;
                    break;
            }
        }
        else//如果碰到墙壁
        {
            switch(direction){
                case RIGHTUP:
                    if(y-r<=0){
                        y+=speed;
                        if(x+r>=fieldX){
                            x-=speed;
                            direction=Direction.LEFTDOWN;
                        }//碰到右上角
                        else{
                            x+=speed;
                            direction= Direction.RIGHTDOWN;
                        }//碰到上边，不是右上角
                    }else{//碰到右边
                        x-=speed;
                        y-=speed;
                        direction= Direction.LEFTUP;
                    }
                    break;
                case RIGHTDOWN:
                    if(x+r>=fieldX && y+r>=fieldY){//碰到右下角
                        x-=speed;
                        y-=speed;
                        direction= Direction.LEFTUP;
                    }else if(x+r>=fieldX)//右边
                    {
                        x-=speed;
                        y+=speed;
                        direction= Direction.LEFTDOWN;
                    }else{
                        x+=speed;
                        y-=speed;
                        direction= Direction.RIGHTUP;
                    }
                    break;
                case LEFTDOWN:
                    if(x-r<=0 && y+r>=fieldY){//左下角
                        x+=speed;
                        y-=speed;
                        direction= Direction.RIGHTUP;
                    }else if(x-r<=0){//左边
                        x+=speed;
                        y+=speed;
                        direction= Direction.RIGHTDOWN;
                    }else{
                        x-=speed;
                        y-=speed;
                        direction= Direction.LEFTUP;
                    }
                    break;
                case LEFTUP:
                    if(x-r<=0 && y-r<=0){//左上角
                        x+=speed;
                        y+=speed;
                        direction= Direction.RIGHTDOWN;
                    }else if(x-r<=0){//左边
                        x+=speed;
                        y-=speed;
                        direction= Direction.RIGHTUP;
                    }else{//上边
                        x-=speed;
                        y+=speed;
                        direction= Direction.LEFTDOWN;
                    }
                    break;
            }
        }
    }
}
